<?php
/**
 * File: VoteController.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-19 22:39
 */

namespace App\Controller;


use App\Core\Request;
use App\Core\WebController;
use App\Models\VoteData;
use App\Models\VoteInfo;
use App\Models\VoteProject;

class VoteController extends WebController
{
    public function support(Request $request)
    {
        $uid = $request->user->uid;
        $vote_id = $request->query->getInt('id');
        $vote = new VoteInfo();
        if ($vote_id < 1 || !($vote_info = $vote->find($vote_id))) {
            ajaxError('作品参数错误');
        }
        if ($vote_info['state'] != 1) {
            ajaxError('您不能为该作品点赞');
        }
        $vote_data = new VoteData();
        //判断是否已经投过票
        if ($this->hasVote($vote_id, $uid, $request)) {
            ajaxSuccess(['count' => $vote_info['vote_count']]);
        }
        $vp = new VoteProject();
        $vp = $vp->find($vote_info['project_id']);
        if (!$vp) ajaxError('作品配置不正确,请稍候重试');
        $voted_count = $vote_data->count('vote_no', ['project_id' => $vote_info['project_id'], 'uid' => $uid]);
        $allow_count = json_decode($vp['allow_vote_count'], 1);
        $allow_count = $allow_count[$request->user->user_role];

        if ($allow_count > 0 && $voted_count >= $allow_count) {
            ajaxError('每人最多可以点赞' . $allow_count . "次\n你已经操作了" . $voted_count . '次');
        }
        try {
            $vote_data->insert([
                'project_id' => $vote_info['project_id'],
                'vote_id' => $vote_id,
                'uid' => $uid,
                'vote_ip' => getClientIP(),
                'vote_count' => $request->user->vote_price
            ]);
            if (!$vote_data->hasError()) {
                $vote->update(['vote_count[+]' => $request->user->vote_price], ['vote_id' => $vote_id]);//更新
                if (!$vote_data->hasError()) {
                    ajaxSuccess(['count' => $vote_info['vote_count'] + $request->user->vote_price]);
                }
            }
        } catch (\Exception $e) {
            ajaxError($e->getMessage());
        }
        ajaxError('点赞失败了');
    }

    private function hasVote($vote_id, $uid, Request $request)
    {
        $vote_data = new VoteData();
        if ($request->user->user_role == 'admin') return false;
        return $vote_data->has(['vote_id' => $vote_id, 'uid' => $uid]);
    }

    public function item(Request $request)
    {
        $vote_id = $request->query->getInt('id');
        $vote = new VoteInfo();
        if ($vote_id < 1 || !($vote_data = $vote->find($vote_id))) {
            $this->message('作品参数错误');
        }
        $this->assign('page_title', $vote_data['vote_name']);
        $this->assign('is_voted', $this->hasVote($vote_id, $request->user->uid, $request));
        $hist = $vote->query("SELECT
	d.*, u.user_name,student_no,phone,
	u.user_open_id
FROM
	vote_data d,
	user_info u
WHERE
	vote_id = '{$vote_id}'
AND d.uid = u.uid
ORDER BY
	created_at DESC")->fetchAll();
        $this->assign('vote_history', $hist);
        $this->render('vote', ['vote' => $vote_data]);
    }
}